package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppPingtaiHuodongService;
import com.vf.admin.client.service.AppRoomAdminService;
import com.vf.admin.client.service.AppSystemService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.miaodong.entity.AppPingtaiHuodong;
import com.vf.admin.miaodong.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppXiTongXiaoXiController.java
 * @Description 平台活动接口
 * @createTime 2021年07月22日 17:52:00
 */
@Controller
@RequestMapping("/app/pingtai")
public class AppPingtaiController extends ClientBaseController {


    @Autowired
    AppPingtaiHuodongService appPingtaiHuodongService;

    @Autowired
    AppSystemService appSystemService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppRoomAdminService appRoomAdminService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * @Description 获取平台活动
     * @Param: [pageno] 页数
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2021/10/22
     */
    @ResponseBody
    @RequestMapping("/pingtaiList")
    public Result pingtaiList(Integer pageno) {
        try {
            if (pageno == null) {
                return this.errorJSON("页码不能为空");
            }
            SplitPageBean splitPageBean = new SplitPageBean(pageno, 10);
            List<AppPingtaiHuodong> pingtaiHuodongList = appPingtaiHuodongService.findList(splitPageBean);
            if (pingtaiHuodongList == null || pingtaiHuodongList.size() == 0) {
                return this.sucessPage(new ArrayList<>(), splitPageBean.getNextPageNo());
            } else {
                return this.sucessPage(pingtaiHuodongList, splitPageBean.getNextPageNo());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * @Description 获取平台活动详情
     * @Param: [pageno] 页数
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2021/10/22
     */
    @ResponseBody
    @RequestMapping("/getPingtai")
    public Result getPingtai(Integer id) {
        try {
            if (id == null) {
                return this.errorJSON("ID不存在");
            }
            AppPingtaiHuodong pingtaiHuodongList = appPingtaiHuodongService.get(id);
            return sucessJSON(pingtaiHuodongList,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

    /**
     * @Description 获取平台活动和系统消息最新一条
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2021/10/22
     */
    @ResponseBody
    @RequestMapping("/getPingtaiAndXitong")
    public Result getPingtaiAndXitong() {
        try {
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //获取平台活动最新一条
            AppPingtaiHuodong pingtaiHuodong = appPingtaiHuodongService.getNew();
            //获取平台活动最新一条
            AppMessageRecord messageRecord = appSystemService.getNew(user.getId());
            Map<String,Object> tomap=new HashMap<>(16);
            tomap.put("pingtai",pingtaiHuodong);
            tomap.put("xitong",messageRecord);
            return sucessJSON(tomap,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }
}
